//
//  FortuneViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/17.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "FortuneViewController.h"
#import "FortuneReusableView.h"
#import "FortuneCollectionViewCell.h"
#import "FortuneModel.h"
#import "FortuneDetailViewController.h"
@interface FortuneViewController ()
@property (nonatomic, strong)NSMutableArray *fortuneArray;
@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)UIActivityIndicatorView *act;
@property (nonatomic, strong)UIAlertView *alert;
@end

@implementation FortuneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"星座宝宝";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(buttonClick)];
    
    BlurImageView *blurImage = [[BlurImageView alloc] initWithFrame:kBounds];
    [self.view addSubview:blurImage];
    [self setCollectionView];
    [self reloadFortune];
    
    self.alert = [[UIAlertView alloc] initWithTitle:@"正在加载..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
    self.act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhite)];
    [_act setFrame:CGRectMake(120, 48, 37, 37)];
    [_alert addSubview:_act];
    [_act startAnimating];
    [_alert show];
}
//请求数据
- (void)reloadFortune{
    [LORequestManger GET:kFortuneUrl success:^(id response) {
        NSArray *array = (NSArray *)response;
        for (NSArray *arr1 in array) {
            for (NSDictionary *dic in arr1) {
                FortuneModel *model = [[FortuneModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.fortuneArray addObject:model];
            }
        }
        [self.collectionView reloadData];
        [self.act stopAnimating];
        [self.alert dismissWithClickedButtonIndex:0 animated:YES];
    } failure:nil];
   

}
- (NSMutableArray *)fortuneArray{
    if (!_fortuneArray) {
        _fortuneArray = [[NSMutableArray alloc] init];
    }
    return _fortuneArray;
}
- (void)buttonClick{
    [self dismissViewControllerAnimated:NO completion:nil];
}
-(void)setCollectionView{
    //创建布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.minimumLineSpacing = 20;
    layout.itemSize = CGSizeMake((kWidth - 80) / 3, (kWidth + 10)/ 3);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 25, 0);
    
    //创建集合视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(25, 64, kWidth - 50, kHeight - 64)collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    //注册
    [self.collectionView registerClass:[FortuneCollectionViewCell class] forCellWithReuseIdentifier:@"fortuneCell"];
    [self.collectionView registerClass:[FortuneReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [self.view addSubview:_collectionView];
}
//增广视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    FortuneReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    reusableView.title = @"12个星座小宝宝";
    return reusableView;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={375,40};
    return size;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.fortuneArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FortuneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fortuneCell" forIndexPath:indexPath];
    FortuneModel *model = self.fortuneArray[indexPath.row];
    [cell.startImage sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"tarot2_crystal_img_017.png"]];
    cell.titleLabel.text = model.sname;
    cell.birthsLabel.text = model.cname;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    FortuneDetailViewController *forVC = [[FortuneDetailViewController alloc] init];
    FortuneModel *model = self.fortuneArray[indexPath.row];
    forVC.model = model;
    [self.navigationController pushViewController:forVC animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
